package com.tianchi.lexiang.adapter;

import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tianchi.lexiang.R;
import com.tianchi.lexiang.bean.AlreadyPoint;
import com.tianchi.lexiang.constant.Commands;
import com.tianchi.lexiang.deep.AlreadyPointScreen;
import com.tianchi.lexiang.service.GetAlreadyPointNums;
import com.tianchi.lexiang.util.AlreadyPointUtil;

public class AlreadyPointAdapter extends BaseAdapter {

	private static final String TAG = "AlreadyPointAdapter";
	private AlreadyPointUtil instence = AlreadyPointUtil.getInstence();
	private GetAlreadyPointNums numInstence = GetAlreadyPointNums.getInstence();

	private LayoutInflater mInflater;

	private Context context;
	private List<AlreadyPoint> alreadyPoints;
	private int currentSingSongId;
	private Handler handler;

	private Animation mScale;

	public AlreadyPointAdapter(Context context, List<AlreadyPoint> alreadyPoints) {
		this.context = context;
		this.alreadyPoints = alreadyPoints;
		mInflater = LayoutInflater.from(context);
		currentSingSongId = instence.getFirstAlreadyPoingSongId();
		mScale = AnimationUtils.loadAnimation(context, R.anim.scale);
	}

	public void setHander(Handler hander) {
		this.handler = hander;
	}

	@Override
	public int getCount() {
		return alreadyPoints.size();
	}

	@Override
	public Object getItem(int arg0) {
		return alreadyPoints.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void changeDataSource(List<AlreadyPoint> list) {
		this.alreadyPoints = list;
		currentSingSongId = instence.getFirstAlreadyPoingSongId();
		notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView != null) {
			holder = (ViewHolder) convertView.getTag();
		} else {
			convertView = mInflater.inflate(R.xml.already_point_item, null);
			holder = new ViewHolder();
			holder.alreadyPointTopBtn = (ImageView) convertView
					.findViewById(R.id.already_point_top_btn);
			holder.alreadyPointDeleteBtn = (ImageView) convertView
					.findViewById(R.id.already_point_delete_btn);
			holder.alreadyPointSongNameTxt = (TextView) convertView
					.findViewById(R.id.already_poing_song_name);
			holder.nowPlayingTxt = (TextView) convertView
					.findViewById(R.id.now_playing);
			convertView.setTag(holder);
		}
		final AlreadyPoint alreadyPointSong = alreadyPoints.get(position);
		Log.d(TAG, "[getView]....currentSingSongId===>" + currentSingSongId);

		if (position == 0
				&& alreadyPointSong.alreadyPointId == currentSingSongId) {
			holder.alreadyPointTopBtn.setVisibility(View.GONE);
			holder.alreadyPointDeleteBtn.setVisibility(View.GONE);
			holder.nowPlayingTxt.setVisibility(View.VISIBLE);
		} else {
			holder.alreadyPointTopBtn.setVisibility(View.VISIBLE);
			holder.alreadyPointDeleteBtn.setVisibility(View.VISIBLE);
			holder.nowPlayingTxt.setVisibility(View.GONE);
		}
		holder.alreadyPointSongNameTxt.setText(alreadyPointSong.songName);
		holder.alreadyPointTopBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				v.startAnimation(mScale);
				if (instence
						.setAlreadyPointSongToTop(alreadyPointSong.alreadyPointId)) {
					numInstence.sendBroadcastUpdateOsdSubTitle(context);
					Log.d(TAG, "handler===>" + handler);
					numInstence.sendBroadcastPendingToSyncServerAlreadyPoint(context, 
							alreadyPointSong.songId, "", alreadyPointSong.alreadyPointOrder,Commands.CMD_DB_TOP);
					handler.sendEmptyMessage(AlreadyPointScreen.INIT_QUERY);
				}
			}
		});
		holder.alreadyPointDeleteBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				v.startAnimation(mScale);
				Log.d(TAG, "alreadyPointSong.alreadyPointId===>" + alreadyPointSong.alreadyPointId);
				if (instence
						.deleteAlreadyPointSong(alreadyPointSong.alreadyPointId)) {
					numInstence.sendBroadcastUpdateAlreadyPointNumber(context);
					numInstence.sendBroadcastUpdateOsdSubTitle(context);
					numInstence.sendBroadcastPendingToSyncServerAlreadyPoint(context, alreadyPointSong.songId, "", alreadyPointSong.alreadyPointOrder,Commands.CMD_DB_DELETE);
					Log.d(TAG, "[getView]   alreadyPoints.size()==>" + alreadyPoints.size());
					if(alreadyPoints.size() == 1) {
						handler.sendEmptyMessage(AlreadyPointScreen.DELETE_REFRESH);
						}
					else
						handler.sendEmptyMessage(AlreadyPointScreen.REFRESH_PAGE);
				}
			}
		});
		return convertView;
	}

	private class ViewHolder {
		TextView alreadyPointSongNameTxt;
		TextView nowPlayingTxt;
		ImageView alreadyPointTopBtn;
		ImageView alreadyPointDeleteBtn;
	}
}
